package fi.profecons.csswindows;

import java.util.Iterator;
import java.util.Vector;

class ScriptCache {
	private Vector script;

	private boolean scripting;
	
	private State state;
	
	public ScriptCache(){
		this.script = new Vector();
		this.scripting = true;
	}
	
	
	protected void clearScript() {
		this.script.clear();
	}

	protected Vector getScript() {
		return this.script;
	}

	protected void scriptingOff() {
		scripting = false;
	}

	protected void scriptingOn() {
		scripting = true;
	}
	
	protected void addCommand(String cmd) {
		if (scripting) {
			if (state == null) {
				this.script.add(cmd);
			} else {
				state.addCommand(cmd);
			}
		}
	}

	protected void addCommands(Vector cmdVector) {
		Iterator i = cmdVector.iterator();
		while (i.hasNext()) {
			String cmd = (String) i.next();
			addCommand(cmd);
		}
	}
	
	protected State getState() {
		return state;
	}

	protected void setState(State state) {
		if (state != null) {
			this.state = state;
			state.addCommands(this.getScript());
			this.clearScript();
		}
	}

}
